package com.free.fs.controller;

import com.free.fs.mapper.UserMapper;
import com.free.fs.model.Message;
import com.free.fs.service.impl.MessageServieImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class MessageController extends BaseController{

    @Autowired
    MessageServieImpl messageServie;


    public Date getCurrentDate() {
        //产生一个不带毫秒的时间,不然,SQL时间和JAVA时间格式不一致
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date tm = new Date();
        try {
            tm= sdf.parse(sdf.format(new Date()));
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        return tm;
    }

    /**
     * 添加留言
     * @return
     */
    @PostMapping("/addMessage")
    @ResponseBody
    public String addMessage(Message message){
        if(message.getRemark() == null || message.getFileId() <= 0){
            System.out.println(message);
            return "false";
        }
        message.setUserId(getLoginUserId());
        message.setRemarkTime(getCurrentDate());
        messageServie.addMessage(message);
        return "true";
    }


    /**
     *留言页面
     */
    @GetMapping("/message/{fileId}")
    public String message(Model model,@PathVariable("fileId") Long fileId) {
        List<Message> messageList = messageServie.queryMessageList(fileId);
        System.out.println(messageList);
        //留言信息
        model.addAttribute("messageList",messageList);
        // 登录用户信息
        model.addAttribute("loginUser", getLoginUser());
        return "message";
    }



}
